﻿using System;
using System.Collections.Generic;
using System.Text;
using GoShopping.Model;
using System.Data;

namespace GoShopping.BLL
{
    /// <summary>
    /// 用户操作类
    /// </summary>
    public class UserBean
    {
        /// <summary>
        /// 获取所有已注册会员
        /// </summary>
        /// <returns></returns>
        public IList<UserInfo> GetAllUserList()
        {
            IList<UserInfo> userList = new List<UserInfo>();
            DataTable dt = Databases.DBGoShopping.Select(GoShoppingNBear.UserInfo).ToDataSet().Tables[0];
            UserInfo[] userArr = NBear.Mapping.ObjectConvertor.ToArray<DataTable, UserInfo>(dt);

            foreach (UserInfo u in userArr)
            {
                userList.Add(u);
            }
            return userList;
        }

        /// <summary>
        /// 获取已注册会员
        /// </summary>
        /// <param name="whereStr">输入查询SQL的Where条件部分,请以AND开始，ex："and userid >10"</param>
        /// <returns></returns>
        public IList<UserInfo> GetUserList(string whereStr)
        {
            string sql = "SELECT * FROM " + GoShoppingNBear.UserInfo.___GetTableName() + " WHERE 1=1 " + whereStr;
            return Databases.DBGoShopping.CustomSql(sql).ToList<UserInfo>();
        }

        /// <summary>
        /// 根据会员名取会员信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public UserInfo GetOneUser(string userName)
        {
            DataTable dt = Databases.DBGoShopping.Select(GoShoppingNBear.UserInfo).Where(GoShoppingNBear.UserInfo.Name == userName).ToDataSet().Tables[0];
            if (dt.Rows.Count == 1)
            {
                UserInfo u = NBear.Mapping.ObjectConvertor.ToObject<DataRow,UserInfo>(dt.Rows[0]);
                return u;
            }
            else
            {
                if (dt.Rows.Count > 1)
                {
                    throw (new Exception("userName is not single!"));
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 根据会员ID取会员信息
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public UserInfo GetOneUser(int userid)
        {
            DataTable dt = Databases.DBGoShopping.Select(GoShoppingNBear.UserInfo).Where(GoShoppingNBear.UserInfo.ID == userid).ToDataSet().Tables[0];
            if (dt.Rows.Count == 1)
            {
                UserInfo u = NBear.Mapping.ObjectConvertor.ToObject<DataRow, UserInfo>(dt.Rows[0]);
                return u;
            }
            else
            {
                if (dt.Rows.Count > 1)
                {
                    throw (new Exception("userName is not single!"));
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 获取用户名
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public string GetUserName(int userid)
        {
            return Databases.DBGoShopping.Select(
                GoShoppingNBear.UserInfo, GoShoppingNBear.UserInfo.Name).Where(
                GoShoppingNBear.UserInfo.ID == userid).ToScalar<string>();
        }

        /// <summary>
        /// 更新会员信息
        /// 会根据UserID更新
        /// [扩展信息]
        /// </summary>
        /// <param name="u"></param>
        /// <returns></returns>
        public bool UpdateUser(UserInfo u)
        {
            int i = Databases.DBGoShopping.Update(GoShoppingNBear.UserInfo).AddColumn(
                GoShoppingNBear.UserInfo.Sex, u.Sex).AddColumn(
                GoShoppingNBear.UserInfo.Birthday, u.Birthday).AddColumn(
                GoShoppingNBear.UserInfo.GiftMoney, u.GiftMoney).AddColumn(
                GoShoppingNBear.UserInfo.Integration, u.Integration).AddColumn(
                GoShoppingNBear.UserInfo.TrueName, u.TrueName).AddColumn(
                GoShoppingNBear.UserInfo.Telephone, u.Telephone).AddColumn(
                GoShoppingNBear.UserInfo.Vocation, u.Vocation).AddColumn(
                GoShoppingNBear.UserInfo.ZipCode, u.ZipCode).Where(GoShoppingNBear.UserInfo.ID == u.ID).Execute();
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 给用户增加积分
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="point">分值</param>
        public void AddIntegrateForUser(string userName, int point)
        {
            int oldPoint = Databases.DBGoShopping.Select(GoShoppingNBear.UserInfo,
                GoShoppingNBear.UserInfo.Integration).ToScalar<int>();

            Databases.DBGoShopping.Update(GoShoppingNBear.UserInfo).AddColumn(
                GoShoppingNBear.UserInfo.Integration, oldPoint + point).Where(
                GoShoppingNBear.UserInfo.Name == userName).Execute();
        }

    }
}
